package com.csh.szhm.activity;

import java.util.ArrayList;
import java.util.List;

import com.csh.szhm.R;
import com.csh.szhm.SzhmApplication;
import com.csh.szhm.custorm.PhotoViewPager;
import com.csh.szhm.custorm.touchview.PhotoView;
import com.csh.szhm.custorm.touchview.PhotoViewAttacher.OnPhotoTapListener;
import com.csh.szhm.http.HttpUrls;
import com.csh.szhm.utils.ActivityStartAndFinshAnimation;
import com.csh.szhm.utils.StringUtil;

import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;

public class SzhmImageDetailActivity extends MyBaseActivity<List<String>> {

	private PhotoViewPager mViewPager;

	private TextView mTextViewNum;

	private RelativeLayout mRoot;

	@Override
	protected void init() {
		mListFace = getIntent().getStringArrayListExtra("array");
		int pos = getIntent().getIntExtra("pos", 0);
		initContentView(R.layout.cee_school_detail_img_layout);
		mViewPager = (PhotoViewPager) findViewById(R.id.img_detail_test_pager);
		mTextViewNum = (TextView) findViewById(R.id.img_num_id);
		mRoot = (RelativeLayout) findViewById(R.id.car_detail_img_root_id);
		mTextViewNum.setText(pos + 1 + "/" + mListFace.size());
		mViewPager.setAdapter(new SamplePagerAdapter());
		mViewPager.setOnPageChangeListener(new GuidePageChangeListener());
		mViewPager.setCurrentItem(pos);
	}

	private ArrayList<String> mListFace = new ArrayList<String>();

	public void click(View v) {
		switch (v.getId()) {
		case R.id.car_detail_back_id:
			finish();
			break;
		case R.id.img_detail_download_id:
			fileName = System.currentTimeMillis() + ".jpg";
			// showDialog();
			// new Thread(saveFileRunnable).start();
			break;
		}
	}

	/**
	 * 标示是否成功与失败
	 */
	private int message;

	private String fileName;

	private int currentPosition = 0;

	// private Runnable saveFileRunnable = new Runnable()
	// {
	//
	// @Override
	// public void run()
	// {
	// try
	// {
	// message =
	// DownLoadImage.saveFile(
	// GlobalUtils.mNWslist.get(currentPosition),
	// fileName);
	// }
	// catch (IOException e)
	// {
	// message = -1;
	// e.printStackTrace();
	// }
	// messageHandler.sendMessage(messageHandler.obtainMessage());
	// }
	// };

	// private Handler messageHandler = new Handler()
	// {
	//
	// @Override
	// public void handleMessage(Message msg)
	// {
	// if (message == 1)
	// {
	// DebugUntil.createInstance().toastStr(
	// "图片保存在" + DownLoadImage.ALBUM_PATH + fileName,
	// CeeSchoolDetailImageActivity.this);
	// }
	// else
	// {
	// DebugUntil.createInstance().toastStr("图片保存失败",
	// CeeSchoolDetailImageActivity.this);
	// }
	// dissDialog();
	// }
	// };

	private class GuidePageChangeListener implements OnPageChangeListener {

		@Override
		public void onPageScrollStateChanged(int arg0) {

		}

		@Override
		public void onPageScrolled(int arg0, float arg1, int arg2) {

		}

		@Override
		public void onPageSelected(int arg0) {
			currentPosition = arg0;
			mTextViewNum.setText((arg0 + 1) + "/" + mListFace.size());
		}
	}

	class MyPageTap implements OnPhotoTapListener {

		@Override
		public void onPhotoTap(View view, float x, float y) {
			if (!mFlagShowHide) {
				ActivityStartAndFinshAnimation.HideAnimation(mRoot);
			} else {
				ActivityStartAndFinshAnimation.ShowAnimation(mRoot);
			}
			mFlagShowHide = !mFlagShowHide;
		}

	}

	private boolean mFlagShowHide;

	class SamplePagerAdapter extends PagerAdapter {

		public SamplePagerAdapter() {
		}

		@Override
		public int getCount() {
			return mListFace.size();
		}

		@Override
		public View instantiateItem(ViewGroup container, int position) {
			PhotoView photoView = new PhotoView(container.getContext());
			photoView.setOnPhotoTapListener(new MyPageTap());
			photoView.setLayoutParams(new LayoutParams(
					LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
			String url = mListFace.get(position);
			if (!StringUtil.isEmptyString(url)) {
				if (!url.contains("http://") && !url.contains("file://")) {
					url = HttpUrls.PATH_ROOT + url;
				}
			} else {
			}
			SzhmApplication.display(url, photoView);
			container.addView(photoView, LayoutParams.MATCH_PARENT,
					LayoutParams.MATCH_PARENT);
			return photoView;
		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			container.removeView((View) object);
		}

		@Override
		public boolean isViewFromObject(View view, Object object) {
			return view == object;
		}
	}

}
